﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BattleCity
{
    public class SurvivalMenu
    {
        public enum Selection
        {
            Continue,
            NewGame,
            Start,
            None
        }

        Vector2[] stringsPositions;
        Vector2 menuWrapperPosition;

        SpriteFont menuFont;
        Texture2D menuWrapper;
        Texture2D button;
        Texture2D tapped;
        SpriteBatch spriteBatch;

        int buttonX;
        bool continueAvailable;

        public bool warningMessage;

        float tappedCounter = 0.0f;
        Selection selected = Selection.None;

        public SurvivalMenu(bool continueAvailable, Vector2 screenCenter, SpriteFont menuFont, Texture2D menuWrapper, Texture2D button, Texture2D tapped, SpriteBatch spriteBatch)
        {
            stringsPositions = new Vector2[6];
            warningMessage = false;

            this.menuWrapper = menuWrapper;
            this.button = button;
            this.tapped = tapped;
            this.menuFont = menuFont;
            this.spriteBatch = spriteBatch;
            this.continueAvailable = continueAvailable;

            if (continueAvailable)
            {
                stringsPositions[0] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.CONTINUE).X * 0.5f, 50.0f);
                stringsPositions[1] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.NEWGAME).X * 0.5f, 160.0f);
            }
            else
            {
                stringsPositions[0] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.NEWGAME).X * 0.5f, 50.0f);
            }

            stringsPositions[2] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.WARNING).X * 0.5f, 50.0f);
            stringsPositions[3] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.WARNING_1).X * 0.5f, 150.0f);
            stringsPositions[4] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.WARNING_2).X * 0.5f, 200.0f);
            stringsPositions[5] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.STARTGAME).X * 0.5f, 380.0f);

            buttonX = (int)screenCenter.X - (button.Width / 2);

            #if WINDOWS_PHONE 
            menuWrapperPosition = new Vector2(screenCenter.X - 200, 0.0f);    
            #else
            menuWrapperPosition = new Vector2(screenCenter.X - 300, 0.0f);
            #endif
        }

        public void Update(Vector2 gsPosition)
        {
            if ((gsPosition.X >= menuWrapperPosition.X) && (gsPosition.X <= menuWrapperPosition.X + menuWrapper.Width))
            {
                if (warningMessage)
                {
                    if (gsPosition.Y > stringsPositions[5].Y - 15.0f) selected = Selection.Start;
                }
                else
                {
                    if (continueAvailable)
                    {
                        if (gsPosition.Y < stringsPositions[0].Y + menuFont.MeasureString(strings.CONTINUE).Y * 1.5f) selected = Selection.Continue;
                        
                        else if (gsPosition.Y < stringsPositions[1].Y + menuFont.MeasureString(strings.NEWGAME).Y * 1.5f)
                        {
                            selected = Selection.NewGame;
                        }
                    }
                    else
                    {
                        if (gsPosition.Y < stringsPositions[0].Y + menuFont.MeasureString(strings.NEWGAME).Y * 1.5f) selected = Selection.NewGame;
                    }
                }

            }

            if (selected != Selection.None)
            {
                if (Options.soundEnabled) Sound.playSound(ref Sound.buttonPressed);
                tappedCounter = 0.2f;
            }
        }

        public void UpdateCounter(MenuManager manager, float dt)
        {
            if (tappedCounter > 0.0f)
            {
                tappedCounter -= dt;

            }

            if ((tappedCounter <= 0.0f) && (selected != Selection.None))
            {
                tappedCounter = 0.0f;
                switch (selected)
                {
                    case Selection.Continue:
                        manager.survivalContinue();
                        break;
                    case Selection.NewGame:
                        if (continueAvailable) warningMessage = true;
                        else manager.survivalStart();
                       break;
                    case Selection.Start:
                        manager.survivalStart();
                        break;
                }
                selected = Selection.None;
            }
        }

        public void Draw()
        {
            spriteBatch.Draw(menuWrapper, menuWrapperPosition, Color.White);
            if (warningMessage)
            {
                spriteBatch.DrawString(menuFont, strings.WARNING, stringsPositions[2], Color.Green);
                spriteBatch.DrawString(menuFont, strings.WARNING_1, stringsPositions[3], Color.Green);
                spriteBatch.DrawString(menuFont, strings.WARNING_2, stringsPositions[4], Color.Green);

                spriteBatch.Draw(getButton(Selection.Start), new Vector2(buttonX, stringsPositions[5].Y - 20), Color.White);
                spriteBatch.DrawString(menuFont, strings.STARTGAME, stringsPositions[5] + new Vector2(2, 2), Color.Black);
                spriteBatch.DrawString(menuFont, strings.STARTGAME, stringsPositions[5], Color.DarkCyan);
            }
            else
            {
                if (continueAvailable)
                {
                    spriteBatch.Draw(getButton(Selection.Continue), new Vector2(buttonX, stringsPositions[0].Y - 20), Color.White);
                    spriteBatch.Draw(getButton(Selection.NewGame), new Vector2(buttonX, stringsPositions[1].Y - 20), Color.White);

                    spriteBatch.DrawString(menuFont, strings.CONTINUE, stringsPositions[0] + new Vector2(2, 2), Color.Black);
                    spriteBatch.DrawString(menuFont, strings.NEWGAME, stringsPositions[1] + new Vector2(2, 2), Color.Black);

                    spriteBatch.DrawString(menuFont, strings.CONTINUE, stringsPositions[0], Color.DarkCyan);
                    spriteBatch.DrawString(menuFont, strings.NEWGAME, stringsPositions[1], Color.DarkCyan);
                }
                else
                {
                    spriteBatch.Draw(getButton(Selection.NewGame), new Vector2(buttonX, stringsPositions[0].Y - 20), Color.White);
                    spriteBatch.DrawString(menuFont, strings.NEWGAME, stringsPositions[0] + new Vector2(2, 2), Color.Black);
                    spriteBatch.DrawString(menuFont, strings.NEWGAME, stringsPositions[0], Color.DarkCyan);
                }
            }
        }

        Texture2D getButton(Selection buttonPressed)
        {
            if (selected == buttonPressed) return tapped;
            else return button;
        }
    }
}
